<?php
namespace app\home\controller;

use app\common\model\FilmModel;
use app\common\model\FilmSourceModel;
use app\common\service\EsSearchService;
use app\common\service\DoubanService;
use app\home\service\SearchProcessService;
use Elasticsearch\ClientBuilder;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use think\Db;
use think\paginator\driver\Bootstrap;

class Film extends \think\Controller
{
    public function index()
    {
        $page = $this->request->get('page', 1, 'intval');
        $pagesize = 10;

        if($page > 100){
            return abort(404);
        }

        $films = FilmModel::where('status', '=', FilmModel::STATUS_NORMAL)->order('id desc')->page($page, $pagesize)->select();
        $total = FilmModel::where('status', '=', FilmModel::STATUS_NORMAL)->cache(true)->count();
        $total = $total > ($pagesize*100) ?  ($pagesize*100) : $total;

        $paginator = new Bootstrap($films, $pagesize, $page, $total, false, ['path' => '/new']);

        $chartHash = 'douban_' . md5(\app\home\service\DoubanService::API_CHART_LIST);
        $chart = cache($chartHash);
        !is_array($chart) && $chart = [];

        $this->assign('chart', $chart);
        $this->assign('page', $paginator->render());
        $this->assign('films', $films);

        $this->assign('meta_desc', 'BTFILM专业电影搜索引擎,海量电影等你来搜,BTFILM提供磁力搜索、种子搜索、影视搜索、电影搜索、最新电影等搜索服务');
        $this->assign('meta_keywords', '最新电影_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_BTFILM搜索');
        $this->assign('meta_title', '最新电影_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_BTFILM搜索');
        return $this->fetch();
    }

    public function search()
    {
        $page = $this->request->get('page', 1, 'intval');
        $sort = $this->request->get('sort', 1, 'intval');
        $keyword = $this->request->get('keyword', '');
        $pagesize = 10;
        if (empty($keyword)) {
            return redirect('/');
        }

        $process = new SearchProcessService();
        $dbFilm = $process->getDoubanFilm($keyword);

        $chartHash = 'douban_' . md5(\app\home\service\DoubanService::API_CHART_LIST);
        $chart = cache($chartHash);
        !is_array($chart) && $chart = [];

        $es = new EsSearchService();
        $res = $es->search($keyword, $page, $pagesize, $sort);
        $total = $res['total'] > ($pagesize * 100) ?  ($pagesize * 100) : $res['total'];

        $paginator = new Bootstrap($res['films'], $pagesize, $page, $total, false, ['query' => ['keyword' => $keyword,], 'path' => '/search']);

        $this->assign('chart', $chart);
        $this->assign('page', $paginator->render());
        $this->assign('keyword', $keyword);
        $this->assign('films', $res['films']);
        $this->assign('dbFilm', $dbFilm);

        $this->assign('meta_desc', '搜索' . $keyword . '相关电影资源,BTFILM专业电影搜索引擎,海量电影等你来搜,BTFILM提供磁力搜索、种子搜索、影视搜索、电影搜索、最新电影等搜索服务');
        $this->assign('meta_keywords', $keyword . '_最新电影_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_BTFILM搜索');
        $this->assign('meta_title', '搜索' . $keyword . '相关电影资源-最新电影_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_BTFILM搜索');
        return $this->fetch();
    }

    public function detail($id = '')
    {
        empty($id) && $id = $this->request->get('id', 0, 'intval');

        $film = FilmModel::get($id);
        if (empty($film) || $film->status == -1) {
            return error_404_page();
        }

        $sources = FilmSourceModel::where('film_id', '=', $id)->field('id,link,type,title')->select();

        $playUrls = [];
        $ciliUrls = [];
        $otherUrls = [];
        foreach ($sources as $key => $value) {
            if ($value['type'] == 3) {
                $playUrls[] = $value;
            } elseif($value['type'] == 1) {
                $ciliUrls[] = $value;
            } else {
                $otherUrls[] = $value;
            }
        }

        $this->assign('playUrls', $playUrls);
        $this->assign('ciliUrls', $ciliUrls);
        $this->assign('otherUrls', $otherUrls);
        $this->assign('film', $film);

        $this->assign('meta_desc', $film->title . '电影资源，BTFILM专业电影搜索引擎,海量电影等你来搜,BTFILM提供磁力搜索、种子搜索、影视搜索、电影搜索、最新电影等搜索服务');
        $this->assign('meta_keywords', $film->title . 'BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_最新电影_BTFILM搜索');
        $this->assign('meta_title', $film->title . '资源_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_最新电影_BTFILM搜索');

        header('Cache-control: max-age=86400');
        return $this->fetch();
    }
}
